package dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import model.ThanhVienModel;
import util.Datautil;

public class ThanhVienDao {
	
	
	public static ThanhVienModel getThanhVienByTenDnAndPassword(String userName, String password){
		ThanhVienModel model = null;
		try {
			String sql = "SELECT * FROM thanhvien WHERE tendn = ? AND matkhau = ?";
			PreparedStatement ps = Datautil.getConnection().prepareStatement(sql);
			ps.setString(1, userName);
			ps.setString(2, password);
			ResultSet rs = ps.executeQuery();
			
			if(rs.next()){
				model = new ThanhVienModel();
				model.setId(rs.getString("id"));
				model.setTendn(rs.getString("tendn"));
				model.setMatkhau(rs.getString("matkhau"));
				model.setVaitro(rs.getString("vaitro"));
				model.setGhichu(rs.getString("ghichu"));
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return model;
	}
	
	
	
	public static ArrayList<ThanhVienModel> getAllThanhvien(){
		ArrayList<ThanhVienModel> listThanhvien = new ArrayList<ThanhVienModel>();
		try {
			String sql = "SELECT * FROM thanhvien";
			ResultSet rs = Datautil.executeQuery(sql);
			while(rs.next()){
				ThanhVienModel model  = new ThanhVienModel();
				model.setId(rs.getString("id"));
				model.setTendn(rs.getString("tendn"));
				model.setMatkhau(rs.getString("matkhau"));
				model.setVaitro(rs.getString("vaitro"));
				model.setGhichu(rs.getString("ghichu"));
				listThanhvien.add(model);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return listThanhvien;
	}
}
